package com.yyy.instagram.common.PullRefreshHeader;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by qqqqqqq on 2016/9/28.
 */


public class MyPullRefreshFrameLayout extends PtrFrameLayout {

    private  float lastX = 0;
    private  float lastY = 0;

    private PullRefreshHeader mPtrClassicHeader;

    public MyPullRefreshFrameLayout(Context context) {
        super(context);
        initViews();
    }

    public MyPullRefreshFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public MyPullRefreshFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }

    private void initViews() {

        mPtrClassicHeader = new PullRefreshHeader(getContext());
        setHeaderView(mPtrClassicHeader);
        addPtrUIHandler(mPtrClassicHeader);
    }

    public PullRefreshHeader getHeader() {
        return mPtrClassicHeader;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                lastX = e.getX();
                lastY = e.getY();
            }
            case MotionEvent.ACTION_MOVE: {
                float x = e.getX();
                float y = e.getY();

                if (Math.abs(x - lastX) > Math.abs(y - lastY)) {
                    return getChildAt(0).onTouchEvent(e);
                } else {
                    return super.dispatchTouchEvent(e);
                }
            }
            case MotionEvent.ACTION_UP:{
                break;
            }
        }
        return super.dispatchTouchEvent(e);
    }
}
